package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.commom.R;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;

import com.itheima.ydd.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @GetMapping("/list")
    public R listByPage(String advposdesc, String advposname, int page, int limit) {
        log.info("查询参数{}", advposdesc, advposname);
        //构造分页构造器对象
        Page page1 = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(advposdesc != null, YddAdvpos::getAdvposdesc, advposdesc);
        queryWrapper.like(advposname != null, YddAdvpos::getAdvposname, advposname);
        //执行分页查询
        yddAdvposService.page(page1, queryWrapper);

        //对查询得到的数据进行扩增返回
        List<YddAdvpos> records = page1.getRecords();
        List<YddAdvposDto> list = records.stream().map((item) -> {
            YddAdvposDto dto = new YddAdvposDto();
            BeanUtils.copyProperties(item, dto);
            //查询广告图数量
            LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(item.getId() != null, YddAdvimg::getAdvimgpos, item.getId());
            int count = yddAdvimgService.count(wrapper);
            dto.setAdvposnum(count);
            return dto;
        }).collect(Collectors.toList());

        PageUtils<YddAdvposDto> pageBean = new PageUtils<>(page1);
        pageBean.setData(list);
        //返回结果
        return R.success("", pageBean);
    }

    @PostMapping("/update")
    public R updateByYddAdvpos(@RequestBody YddAdvpos yddAdvpos) {
        log.info("yddAdvpos: {}", yddAdvpos);
        yddAdvposService.updateById(yddAdvpos);
        return R.success("修改成功", "");
    }

    @GetMapping("/rowInfo")
    public R rowInfo(Integer id) {
        log.info("id: {}", id);
        YddAdvpos byId = yddAdvposService.getById(id);
        return R.success("", byId);
    }

    @GetMapping("/del")
    public R deleteByYddAdvpos(Integer id) {
        log.info("id: {}", id);
        //判断DefaultData是否为1，如果为默认值1则不能删除
        YddAdvpos byId = yddAdvposService.getById(id);
        if (byId.getDefaultData() != null && byId.getDefaultData().equals("1")) {
            return R.error("不能删除");
        }
        //判断广告数量是否为0，为0可以删，反之不能
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(byId.getId() != null, YddAdvimg::getAdvimgpos, byId.getId());
        int count = yddAdvimgService.count(wrapper);
        if (count != 0) {
            return R.error("广告数量不为0不能删除");
        }
        yddAdvposService.removeById(id);
        return R.success("删除成功", "");
    }
    @PostMapping("/add")
    public R addByYddAdvpos(@RequestBody YddAdvpos yddAdvpos){
        log.info("yddAdvpos: {}",yddAdvpos);
        yddAdvposService.save(yddAdvpos);
        return R.success("添加成功","");
    }
    @GetMapping("all")
    public R all() {
        List<YddAdvpos> list = yddAdvposService.list();
        return R.success("", list);
    }
}
